/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.support.custom.component;

/**
 *
 * @author renan_luz
 */
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class DirectoryChooserPanel extends JComponent {

    private final JTextField directoryField;
    private final JButton chooseDirectoryButton;
    private File directoryChosen;
    private JFileChooser chooser;
    private Set dirChangeListeners;

    public DirectoryChooserPanel() {
        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        directoryField = new JTextField();
        directoryField.setEditable(false);

        chooseDirectoryButton = new JButton("...");
        chooseDirectoryButton.addActionListener(new Handler());

        createControl();

        dirChangeListeners = new HashSet();
    }

    private void createControl() {
        setLayout(new FormLayout("fill:1dlu:grow, 2dlu, fill:pref:nogrow", "default"));
        add(directoryField, new CellConstraints(1, 1));
        add(chooseDirectoryButton, new CellConstraints(3, 1));
    }

    public File getDirectoryChosen() {
        return directoryChosen;
    }

    public void setReadOnly(boolean readOnly) {
        chooseDirectoryButton.setEnabled(!readOnly);
    }

    public void setEnabled(boolean enabled) {
        chooseDirectoryButton.setEnabled(enabled);
        directoryField.setEnabled(enabled);
    }

    public void setDirectoryChosen(File directoryChosen) {
        notifyListeners(this.directoryChosen, directoryChosen);
        this.directoryChosen = directoryChosen;
        directoryField.setText(directoryChosen.getAbsolutePath());
    }

    public void addFileListener(PropertyChangeListener listener) {
        dirChangeListeners.add(listener);
    }

    public void removeFileListener(PropertyChangeListener listener) {
        dirChangeListeners.remove(listener);
    }

    public void notifyListeners(File oldFile, File newFile) {
        PropertyChangeEvent event = new PropertyChangeEvent(this, "directoryChosen", oldFile, newFile);
        Iterator listenerIterator = dirChangeListeners.iterator();
        while (listenerIterator.hasNext()) {
            PropertyChangeListener listener = (PropertyChangeListener) listenerIterator.next();
            listener.propertyChange(event);
        }
    }

    class Handler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                setDirectoryChosen(chooser.getSelectedFile());
            }
        }
    }
}  
